package com.simdo.g768;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.simdo.g768.Util.AppLog;

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //保持屏幕唤醒
        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //window.setNavigationBarColor(Color.parseColor("#87bee0"));
        //window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @SuppressWarnings("deprecation")
    public Spanned fromHtml(String source) {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY) : Html.fromHtml(source);
    }

    public void hideSoftKeyBoard(View view) {
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public Dialog createDialog(boolean isMatch) {
        Dialog mDialog = new Dialog(this, isMatch ? R.style.Theme_B97621 : R.style.style_dialog);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        mDialog.setCancelable(true);   // 返回键不消失
        return mDialog;
    }

    public void showDialog(Dialog mDialog, View view, boolean isMatchParent) {
        showDialog(mDialog, view, isMatchParent, false);
    }

    public void showDialog(Dialog mDialog, View view, boolean isMatchParent, boolean setHeight) {
        //创建最外层Dialog
        mDialog.setContentView(view);
        Window window = mDialog.getWindow();
        if (isMatchParent) {
            window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
            window.getDecorView().setPadding(0, 0, 0, 0); // 消除边距
            WindowManager.LayoutParams lp = window.getAttributes();
            window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.setNavigationBarColor(Color.parseColor("#2A72FF"));
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;   // 设置宽度充满屏幕
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(lp);
        } else {
            window.setGravity(Gravity.CENTER); //可设置dialog的位置
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = getResources().getDisplayMetrics().widthPixels * 6 / 7;// 设置宽度屏幕的 6 / 7
            if (setHeight) lp.height = getResources().getDisplayMetrics().heightPixels * 3 / 4;
            else lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }
        mDialog.show();
    }

    public void closeDialog(Dialog mDialog) {
        if (mDialog != null) mDialog.dismiss();
    }

    /**
     * 仅显示提示信息
     */
    Dialog mRemindDialog;

    public void showRemindDialog(String title, String msg) {
        if (mRemindDialog != null) {
            mRemindDialog.dismiss();
            mRemindDialog = null;
        }
        mRemindDialog = createDialog(false);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_reminder, null);
        TextView tv_title = view.findViewById(R.id.title_reminder);
        TextView tv_msg = view.findViewById(R.id.tv_reminder_msg);
        tv_title.setText(title);
        tv_msg.setText(msg);
        view.findViewById(R.id.btn_reminder_know).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeDialog(mRemindDialog);
                mRemindDialog = null;
            }
        });

        showDialog(mRemindDialog, view, false);
    }

    public void showToast(final String msg) {
        showToast(msg, false);
    }

    public void showToast(final String msg, boolean isLongShow) {
        Context context = getApplicationContext();
        Toast toast = new Toast(context);
        //创建Toast中的文字
        TextView textView = new TextView(context);
        textView.setText(msg);
        textView.setBackgroundResource(R.drawable.radio_main);
        textView.setTextColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(14);
        textView.setPadding(24, 14, 24, 14);
        toast.setView(textView); //把layout设置进入Toast
        toast.setGravity(Gravity.BOTTOM, 0, 200);
        toast.setDuration(isLongShow ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
        toast.show();
    }
}
